package com.xukang.batch.service;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * 流程定义操作
 */
@Service
public class ProcessRepositoryService {

    @Autowired
    private ProcessEngine processEngine;

    /**
     * 从类路径部署单个 BPMN 文件
     */
    public void deployProcessFromClasspath(String bpmnResourcePath) {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource(bpmnResourcePath)  // 加载类路径下的 BPMN 文件
                .name("My Process Deployment")          // 部署名称
                .category("Workflow Processes")         // 部署分类
                .deploy();                              // 执行部署

        System.out.println("部署成功: ID=" + deployment.getId() + ", 名称=" + deployment.getName());
    }

    /**
     * 从文件系统部署 BPMN 文件
     */
    public void deployProcessFromFile(String filePath) throws IOException {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        File file = new File(filePath);

        try (FileInputStream inputStream = new FileInputStream(file)) {
            Deployment deployment = repositoryService.createDeployment()
                    .addInputStream(file.getName(), inputStream)  // 从输入流加载
                    .deploy();

            System.out.println("部署成功: ID=" + deployment.getId());
        }
    }

    /**
     * 展示所有流程
     */
    public void listDeployedProcesses() {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery()
                .orderByProcessDefinitionName()
                .asc()
                .list();

        for (ProcessDefinition definition : processDefinitions) {
            System.out.println("流程定义: " + definition.getName()
                    + ", 版本: " + definition.getVersion()
                    + ", 部署ID: " + definition.getDeploymentId());
        }
    }

    /**
     * 根据部署 ID 删除流程定义（级联删除历史数据）
     */
    public void deleteDeployment(String deploymentId) {
        RepositoryService repositoryService = processEngine.getRepositoryService();

        // true 表示级联删除，会同时删除相关的历史数据和运行中的实例
        repositoryService.deleteDeployment(deploymentId, true);
        System.out.println("部署 " + deploymentId + " 已删除");
    }
    /**
     * 根据流程定义 ID 删除
     */
    public void deleteProcessDefinition(String processDefinitionId) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        String deploymentId = repositoryService.getProcessDefinition(processDefinitionId).getDeploymentId();
        repositoryService.deleteDeployment(deploymentId, true);
    }



}